package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.ZenbuForm;
import com.amsp.oa.oademo.service.ZenbuFormService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/zenbu")
@Api(tags = "人员增补申请表单管理")
public class ZenbuFormController {

    @Autowired
    private ZenbuFormService zenbuFormService;

    @PostMapping("/save")
    @ApiOperation("保存人员增补表单")
    public ResponseEntity<?> save(@RequestBody ZenbuForm form) {
        try {
            boolean isUpdate = form.getId() != null;
            if (isUpdate) {
                ZenbuForm existingRecord = zenbuFormService.getById(form.getId());
                if (existingRecord == null) {
                    return ResponseEntity.badRequest().body("更新失败：记录不存在");
                }
                zenbuFormService.updateById(form);
                return ResponseEntity.ok().body("更新成功");
            } else {
                form.setId(null);
                zenbuFormService.save(form);
                return ResponseEntity.ok().body("保存成功");
            }
        } catch (Exception e) {
            log.error("保存全部表单失败", e);
            return ResponseEntity.badRequest().body("保存失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @ApiOperation("获取人员增补列表")
    public ResponseEntity<?> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String fillDate,
            @RequestParam(required = false) String applicationDepartment,
            @RequestParam(required = false) String applicant,
            @RequestParam(required = false) String employeeType) {
        try {
            Page<ZenbuForm> page = new Page<>(current, size);
            QueryWrapper<ZenbuForm> queryWrapper = new QueryWrapper<>();

            if (StringUtils.hasText(fillDate)) {
                queryWrapper.eq("fill_date", fillDate);
            }
            if (StringUtils.hasText(applicationDepartment)) {
                queryWrapper.eq("application_department", applicationDepartment);
            }
            if (StringUtils.hasText(applicant)) {
                queryWrapper.like("applicant", applicant);
            }
            if (StringUtils.hasText(employeeType)) {
                queryWrapper.eq("employee_type", employeeType);
            }

            // 按填单日期倒序排序
            queryWrapper.orderByDesc("fill_date");

            Page<ZenbuForm> result = zenbuFormService.page(page, queryWrapper);
            return ResponseEntity.ok().body(result);
        } catch (Exception e) {
            log.error("获取全部表单列表失败", e);
            return ResponseEntity.badRequest().body("获取列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/view/{id}")
    @ApiOperation("查看人员增补表单详情")
    public ResponseEntity<?> view(@PathVariable Long id) {
        try {
            ZenbuForm form = zenbuFormService.getById(id);
            if (form == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok().body(form);
        } catch (Exception e) {
            log.error("查看人员增补表单详情失败", e);
            return ResponseEntity.badRequest().body("获取详情失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除人员增补表单")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        try {
            boolean success = zenbuFormService.removeById(id);
            if (success) {
                return ResponseEntity.ok().body("删除成功");
            } else {
                return ResponseEntity.badRequest().body("删除失败：记录不存在");
            }
        } catch (Exception e) {
            log.error("删除人员增补表单失败", e);
            return ResponseEntity.badRequest().body("删除失败: " + e.getMessage());
        }
    }
}